package com.xcf.controller.business.common;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xcf.controller.base.BaseController;
import com.xcf.service.business.common.suitcasestation.SuitcaseStationService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/SuitcaseStation")
public class SuitcaseStationController extends BaseController{
	@Resource
	private SuitcaseStationService suitcaseStationService;
	@RequestMapping(value="/list")
	public ModelAndView suitcaseStationList()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/common/suitcasestation/suitcase_station_list");
		return view;
		
	}
	
	/**
	 * 往前端传值
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/value")
	public void entityMaintainListInfo(HttpServletResponse response)throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
//		List<PageData> entityMainTain = entityMaintainService.findEntityMaintainInfo(pd);
		List<PageData> entityMainTain = suitcaseStationService.findSuitcaseStationInfo(pd);
		JSONArray json = new JSONArray();
		for(PageData p : entityMainTain){
			 JSONObject jo = new JSONObject();
//			 	jo.put("id", p.getString("PARENT_DEPARTMENT_ID"));	  
//	            jo.put("text", p.getString("PARENT_DEPARTMENT_NAME"));
			 jo.put("ID", p.get("ID"));
			 jo.put("FAST_SEARCH_CODE", p.getString("FAST_SEARCH_CODE"));
			 jo.put("STATION_NAME", p.getString("STATION_NAME"));
			 jo.put("ENABLE", p.get("ENABLE"));
			 if(p.get("ENABLE")!=null && p.get("ENABLE")!=""){
				 
			 int i=Integer.parseInt(p.get("ENABLE").toString());
			 if(i==1){
				 jo.put("ENABLE","是");
			 }else{
				 jo.put("ENABLE","否");
			 }
			 }
			
			 

	            json.add(jo);
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 保存提箱场站信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveOrUpdateSuitcaseStationInfo")
	@ResponseBody
	public String saveOrUpdateSuitcaseStationInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();

		if(pd.getString("ID").isEmpty()){
			suitcaseStationService.saveSuitcaseStationInfo(pd);
			}else{
				suitcaseStationService.updataSuitcaseStationInfo(pd);
			}
		return null;
		
	}
	/**
	 * 删除提箱场站信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteSuitcaseStationInfo")
	@ResponseBody
	public String deleteSuitcaseStationInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();

		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			suitcaseStationService.deleteSuitcaseStationInfo(pdq);
		}
		return null;
		
	}
}
